package com.nulldev.util.web.HttpServer3.extensions.nCaptcha.captchas.auto;

import com.nulldev.util.web.HttpServer3.Response;
import com.nulldev.util.web.HttpServer3.extensions.nCaptcha.backend.user.User;
import com.nulldev.util.web.HttpServer3.extensions.nCaptcha.backend.util.UUIDCookieLoader;
import com.nulldev.util.web.HttpServer3.legacyPages.v1.RequestType;
import com.nulldev.util.web.HttpServer3.pages.v3.HttpPage3;

public class AutoPayloadHoneypot extends HttpPage3 {

	public AutoPayloadHoneypot() {
		super("/captcha/signup_submit_form", true);
		super.setCachingMethod(CachingMethod.NO_CACHE);
	}

	/**
	 * This is a honeypot backend for a fake form, added in nCaptcha 3.2.1. <br>
	 * Don't send a POST request unless you want to automatically be marked as a
	 * bot.
	 */

	@Override
	public Response onRequest(final Request request) {
		if (!request.getType().equals(RequestType.POST)) {
			return new Response(405);
		}
		if (!request.hasInputStream()) {
			return new Response(411);
		}

		if (UUIDCookieLoader.hasCookie(request)) {
			final User u = UUIDCookieLoader.userFromRequest(request);
			if (u != null)
				u.markAsBot();
		}

		return new Response(200, blankCopyable(), blankNIOCopyable());
	}

}
